/* Includes ------------------------------------------------------------------*/
#include "MyApplication.h"

/* Private define-------------------------------------------------------------*/

/* Private variables----------------------------------------------------------*/
static void Start_Or_Stop(void);
static void Direction_Adjust(void);
static void Speed_Adjust(Speed_Change_t );
static void Step_One_Pulse(void);
/* Public variables-----------------------------------------------------------*/
Uniplar_Step_Motor_t Uniplar_Step_Motor = 
{
    Stop_Status,
    Forward_Status,
    Speed_6,
    0,
    Drive_Mode_8_Beats,
    0,
    (uint16_t)4096,

    Start_Or_Stop,
    Direction_Adjust,
    Speed_Adjust,
    Step_One_Pulse
};
/* Private function prototypes------------------------------------------------*/
 
/*
* @name   Start_Or_Stop
* @brief  启动或停止步进电机
* @param  None
* @retval None   
*/
static void Start_Or_Stop()
{   
    //如果本来是启动状态
    if(Uniplar_Step_Motor.Status == Start_Status)
    {
        //改变步进电机状态
        Uniplar_Step_Motor.Status = Stop_Status;
        //关闭步进电机,单片机引脚输出低电平，经过ULN2003A反相器，输出高电平，线圈不导电
        CLR_Motor_A;
        CLR_Motor_B;
        CLR_Motor_C;
        CLR_Motor_D;
        Uniplar_Step_Motor.Circle = 0;  //圈数清零
        Display.Disp_Hex(Disp_NUM_6,Uniplar_Step_Motor.Circle,Disp_DP_OFF); //显示圈数
        LED.LED_Fun(LED2,LED_OFF);      //关闭电机运行指示灯
    }
    else    //本来是停止状态
    {
        //改变步进电机状态
        Uniplar_Step_Motor.Status = Start_Status;
        Uniplar_Step_Motor.Circle = Circle_Set_Value;   //设置转动的圈数
        Uniplar_Step_Motor.Pulse_Cnt = 0;               //脉冲数清零
        Display.Disp_Hex(Disp_NUM_6,Uniplar_Step_Motor.Circle,Disp_DP_OFF); //显示圈数
        LED.LED_Fun(LED2,LED_ON);       //开启电机运行指示灯
    }
}

/*
* @name   Direction_Adjust
* @brief  控制步进电机正转或反转
* @param  None
* @retval None   
*/
static void Direction_Adjust()
{
    if(Uniplar_Step_Motor.Status == Start_Status)
    {
        if(Uniplar_Step_Motor.Direction == Forward_Status)
        {
            Uniplar_Step_Motor.Direction = Reverse_Status;      //反转
        }
        else
        {
            Uniplar_Step_Motor.Direction = Forward_Status;      //正转
        }
        Uniplar_Step_Motor.Circle = Circle_Set_Value;   //设置转动的圈数
        Uniplar_Step_Motor.Pulse_Cnt = 0;               //脉冲数清零
        Display.Disp_Hex(Disp_NUM_6,Uniplar_Step_Motor.Circle,Disp_DP_OFF); //重新显示圈数
    }
}

/*
* @name   Speed_Adjust
* @brief  控制步进电机加减速
* @param  Speed_Change：选择加速还是减速
* @retval None   
*/
static void Speed_Adjust(Speed_Change_t Speed_Change)
{
    uint8_t Speed_Temp;
    //判断电机是否在启动状态
    if(Uniplar_Step_Motor.Status == Start_Status)
    {
        //加速
        if(Speed_Change == Speed_up)
        {
            switch (Uniplar_Step_Motor.Speed)
            {
                case Speed_1: Uniplar_Step_Motor.Speed = Speed_2; Speed_Temp = 2; break;
                case Speed_2: Uniplar_Step_Motor.Speed = Speed_3; Speed_Temp = 3; break;
                case Speed_3: Uniplar_Step_Motor.Speed = Speed_4; Speed_Temp = 4; break;
                case Speed_4: Uniplar_Step_Motor.Speed = Speed_5; Speed_Temp = 5; break;
                case Speed_5: Uniplar_Step_Motor.Speed = Speed_6; Speed_Temp = 6; break;
                case Speed_6: Uniplar_Step_Motor.Speed = Speed_7; Speed_Temp = 7; break;
                case Speed_7: Uniplar_Step_Motor.Speed = Speed_8; Speed_Temp = 8; break;
                case Speed_8: Uniplar_Step_Motor.Speed = Speed_9; Speed_Temp = 9; break;
                case Speed_9: Uniplar_Step_Motor.Speed = Speed_9; Speed_Temp = 9; break;
                default: Uniplar_Step_Motor.Speed = Speed_6; break;
            }
        }
        else
        {
            switch (Uniplar_Step_Motor.Speed)
            {
                case Speed_9: Uniplar_Step_Motor.Speed = Speed_8; Speed_Temp = 8; break;
                case Speed_8: Uniplar_Step_Motor.Speed = Speed_7; Speed_Temp = 7; break;
                case Speed_7: Uniplar_Step_Motor.Speed = Speed_6; Speed_Temp = 6; break;
                case Speed_6: Uniplar_Step_Motor.Speed = Speed_5; Speed_Temp = 5; break;
                case Speed_5: Uniplar_Step_Motor.Speed = Speed_4; Speed_Temp = 4; break;
                case Speed_4: Uniplar_Step_Motor.Speed = Speed_3; Speed_Temp = 3; break;
                case Speed_3: Uniplar_Step_Motor.Speed = Speed_2; Speed_Temp = 2; break;
                case Speed_2: Uniplar_Step_Motor.Speed = Speed_1; Speed_Temp = 1; break;
                case Speed_1: Uniplar_Step_Motor.Speed = Speed_1; Speed_Temp = 1; break;
                default: Uniplar_Step_Motor.Speed = Speed_6;break;
            }
        }
        //更新定时器7ARR的值，改变脉冲频率
        TIM7->ARR = Uniplar_Step_Motor.Speed;
        //数码管显示步进电机速度
        Display.Disp_Hex(Disp_NUM_1,Speed_Temp,Disp_DP_OFF);
    }
}


/*
* @name   Step_One_Pulse
* @brief  步进电机步进一个脉冲
* @param  None
* @retval None   
*/
static void Step_One_Pulse()
{
    static uint8_t position = 0;
    //单四拍
    if(Uniplar_Step_Motor.Drive_Mode == Drive_Mode_Single_4_Beats)
    {
        //方向为反转
        if(Uniplar_Step_Motor.Direction == Reverse_Status)
        {
            //反转——逆时针
            switch (position)
            {
                case 0: SET_Motor_A;CLR_Motor_B;CLR_Motor_C;CLR_Motor_D; break;     //A
                case 1: CLR_Motor_A;SET_Motor_B;CLR_Motor_C;CLR_Motor_D; break;     //B
                case 2: CLR_Motor_A;CLR_Motor_B;SET_Motor_C;CLR_Motor_D; break;     //C
                case 3: CLR_Motor_A;CLR_Motor_B;CLR_Motor_C;SET_Motor_D; break;     //D
                default:System.Error_Handler(); break;
            }
        }
        else    //方向为正转
        {
            //正转——顺时针
            switch (position)
            {
                case 0: CLR_Motor_A;CLR_Motor_B;CLR_Motor_C;SET_Motor_D; break;     //D
                case 1: CLR_Motor_A;CLR_Motor_B;SET_Motor_C;CLR_Motor_D; break;     //C
                case 2: CLR_Motor_A;SET_Motor_B;CLR_Motor_C;CLR_Motor_D; break;     //B
                case 3: SET_Motor_A;CLR_Motor_B;CLR_Motor_C;CLR_Motor_D; break;     //A
                default:System.Error_Handler(); break;
            }
        }
        if(++position == 4){position = 0;}  //循环4个脉冲
    }
    //双四拍
    if(Uniplar_Step_Motor.Drive_Mode == Drive_Mode_Double_4_Beats)
    {
        //方向为反转
        if(Uniplar_Step_Motor.Direction == Reverse_Status)
        {
            //反转——逆时针
            switch (position)
            {
                case 0: SET_Motor_A;CLR_Motor_B;CLR_Motor_C;SET_Motor_D; break;     //DA
                case 1: SET_Motor_A;SET_Motor_B;CLR_Motor_C;CLR_Motor_D; break;     //AB
                case 2: CLR_Motor_A;SET_Motor_B;SET_Motor_C;CLR_Motor_D; break;     //BC
                case 3: CLR_Motor_A;CLR_Motor_B;SET_Motor_C;SET_Motor_D; break;     //CD
                default:System.Error_Handler(); break;
            }
        }
        else    //方向为正转
        {
            //正转——顺时针
            switch (position)
            {
                case 0: SET_Motor_A;CLR_Motor_B;CLR_Motor_C;SET_Motor_D; break;     //DA
                case 1: CLR_Motor_A;CLR_Motor_B;SET_Motor_C;SET_Motor_D; break;     //CD
                case 2: CLR_Motor_A;SET_Motor_B;SET_Motor_C;CLR_Motor_D; break;     //BC
                case 3: SET_Motor_A;SET_Motor_B;CLR_Motor_C;CLR_Motor_D; break;     //AB
                default:System.Error_Handler(); break;
            }
        }
        if(++position == 4){position = 0;}  //循环4个脉冲
    }
    //八拍
    if(Uniplar_Step_Motor.Drive_Mode == Drive_Mode_8_Beats)
    {
        //方向为反转
        if(Uniplar_Step_Motor.Direction == Reverse_Status)
        {
            //反转——逆时针
            switch (position)
            {
                case 0: SET_Motor_A;CLR_Motor_B;CLR_Motor_C;CLR_Motor_D; break;     //A
                case 1: SET_Motor_A;SET_Motor_B;CLR_Motor_C;CLR_Motor_D; break;     //AB
                case 2: CLR_Motor_A;SET_Motor_B;CLR_Motor_C;CLR_Motor_D; break;     //B
                case 3: CLR_Motor_A;SET_Motor_B;SET_Motor_C;CLR_Motor_D; break;     //BC
                case 4: CLR_Motor_A;CLR_Motor_B;SET_Motor_C;CLR_Motor_D; break;     //C
                case 5: CLR_Motor_A;CLR_Motor_B;SET_Motor_C;SET_Motor_D; break;     //CD
                case 6: CLR_Motor_A;CLR_Motor_B;CLR_Motor_C;SET_Motor_D; break;     //D
                case 7: SET_Motor_A;CLR_Motor_B;CLR_Motor_C;SET_Motor_D; break;     //DA
                default:System.Error_Handler(); break;
            }
        }
        else    //方向为正转
        {
            //正转——顺时针
            switch (position)
            {
                case 0: SET_Motor_A;CLR_Motor_B;CLR_Motor_C;CLR_Motor_D; break;     //A
                case 1: SET_Motor_A;CLR_Motor_B;CLR_Motor_C;SET_Motor_D; break;     //DA
                case 2: CLR_Motor_A;CLR_Motor_B;CLR_Motor_C;SET_Motor_D; break;     //D
                case 3: CLR_Motor_A;CLR_Motor_B;SET_Motor_C;SET_Motor_D; break;     //CD
                case 4: CLR_Motor_A;CLR_Motor_B;SET_Motor_C;CLR_Motor_D; break;     //C
                case 5: CLR_Motor_A;SET_Motor_B;SET_Motor_C;CLR_Motor_D; break;     //BC
                case 6: CLR_Motor_A;SET_Motor_B;CLR_Motor_C;CLR_Motor_D; break;     //B
                case 7: SET_Motor_A;SET_Motor_B;CLR_Motor_C;CLR_Motor_D; break;     //AB
                default:System.Error_Handler(); break;
            }
        }
        if(++position == 8){position = 0;}  //循环8个脉冲
    }
}
/********************************************************
  End Of File
********************************************************/
